package com.dongjiantong.web.protal.controller.farmdataanalysis;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongjiantong.biz.service.FarmStockService;
import com.dongjiantong.common.model.Response;
import com.dongjiantong.common.web.sso.LoginInfo;
import com.dongjiantong.common.web.util.UserUtil;
import com.dongjiantong.openplatform.annotations.OpenBean;
import com.dongjiantong.openplatform.annotations.OpenMethod;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;
import java.util.Map;

/**
 * Created by mz on 16/1/18.
 *
 */
@Slf4j
@OpenBean
public class FarmDataAnalysisWebController {

    @Reference(version = "1.0.0")
    private FarmStockService farmStockService;

    @OpenMethod(
            key = "web.get.current.stock.by.breed.type",
            httpMethods = RequestMethod.GET
    )
    public Response<Map<String , Map<String , Object>>> getCurrentStockByBreedType(){

        try{
            LoginInfo loginInfo  = UserUtil.getCurrentUser();
            Map<String , Map<String , Object>> resultMap = Maps.newHashMap();
            List<Map<String , Object>> currentProvince = farmStockService.getStockByBreedType(loginInfo.getProvinceId(),null,null,null,null);
            List<Map<String , Object>> currentCity = farmStockService.getStockByBreedType(loginInfo.getProvinceId(),loginInfo.getCityId(),null,null,null);

            Map<String , Object> cityMap = Maps.newHashMap();
            for(Map<String , Object> map : currentCity){
                if(((String)map.get("animalTwoName")).trim().equals("猪")) cityMap.put("pig",map.getOrDefault("stock",0));
                if(((String)map.get("animalTwoName")).trim().equals("牛")) cityMap.put("cow",map.getOrDefault("stock",0));
                if(((String)map.get("animalTwoName")).trim().equals("羊")) cityMap.put("sheep",map.getOrDefault("stock",0));
            }

            Map<String , Object> provinceMap = Maps.newHashMap();
            for(Map<String , Object> map : currentCity){
                if(((String)map.get("animalTwoName")).trim().equals("猪")) provinceMap.put("pig",map.getOrDefault("stock",0));
                if(((String)map.get("animalTwoName")).trim().equals("牛")) provinceMap.put("cow",map.getOrDefault("stock",0));
                if(((String)map.get("animalTwoName")).trim().equals("羊")) provinceMap.put("sheep",map.getOrDefault("stock",0));
            }

            resultMap.put("currentProvince",provinceMap);
            resultMap.put("currentCity",cityMap);
            return Response.ok(resultMap);
        }catch (Exception e){
            log.error(e.getMessage());
            return Response.fail(e.getMessage());
        }
    }
}
